package com.myapp.servlet;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.myapp.sqlmapper.SQLUtil;
import com.myapp.util.CommUtil;
import com.myapp.util.Consts;
import com.myapp.util.DBUtil;
import com.myapp.util.JsonUtil;
import com.myapp.util.MD5;

public class UserReg extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6904246525901097343L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		String nickName = request.getParameter("nickName");
		String machineID = request.getParameter("machineID");
		String companyID = request.getParameter("companyID");
		String password = request.getParameter("password");
		machineID = machineID == null ? "" : machineID;
		password = password == null ? "" : password;
		if (CommUtil.isBlank(nickName) || CommUtil.isBlank(password)) {
			String result = CommUtil.setResultInfo(jsonMap, Consts.CHECK_CODE, Consts.ARG_DESCRIBE);
			CommUtil.responseJson(result, response);
			System.out.println("/test 昵称或密码为空 ");
			return;
		}
		List<String> tempList = new ArrayList<String>();
		tempList.add(nickName);
		String getUserByNameSql = SQLUtil.getSql("userMapper.getUserNumByName", tempList);
		try {
			ResultSet rs = DBUtil.exeQuery(getUserByNameSql);
			int i = 0;
			while (rs.next()) {
				i = Integer.parseInt(rs.getString("usercount"));
				break;
			}
			if (i > 0) {
				String result = CommUtil.setResultInfo(jsonMap, Consts.CHECK_CODE, "user.is.exist");
				CommUtil.responseJson(result, response);
				return;
			}
			password = MD5.md5(password);
			List<String> paramList = new ArrayList<String>();
			paramList.add(nickName);
			paramList.add(machineID);
			paramList.add(companyID);
			paramList.add(password);
			String sql = SQLUtil.getSql("userMapper.insertUser", paramList);
			// String sql =
			// "insert into t_user(nick_name,machine_id,company_id,createdt) values('"+nickName+"','"+machineID+"',"+companyID+",sysdate())";
			DBUtil.exeUpdate(sql);
			Map<String, String> retMap = new HashMap<String, String>();
			// 查询用户信息
			List<String> tempSelect = new ArrayList<String>();
			tempSelect.add(nickName);
			String selectUserSql = SQLUtil.getSql("userMapper.getUserMsgByName", tempSelect);
			ResultSet regUserRS = DBUtil.exeQuery(selectUserSql);
			// 有结果集
			while (regUserRS.next()) {
				retMap.put("id", regUserRS.getString("id"));
				retMap.put("machineID", CommUtil.isNull(regUserRS.getString("machine_id")));
				retMap.put("companyID", CommUtil.isNull(regUserRS.getString("company_id")));
				retMap.put("createDT", regUserRS.getString("createdt"));
				retMap.put("lastLoginDT", CommUtil.isNull(regUserRS.getString("lastlogindt")));
				retMap.put("speakFlag", regUserRS.getString("speakFlag"));
				retMap.put("rescode", Consts.SUCCESS_CODE);
				retMap.put("resmsg", Consts.SUCCESS_DESCRIBE);
				String result = JsonUtil.getJsonStringFromMap(retMap);
				CommUtil.responseJson(result, response);
				return;
			}

			// 无结果集
			String result = CommUtil.setResultInfo(jsonMap, Consts.CHECK_CODE, "reg.success.get.user.error");
			CommUtil.responseJson(result, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			String result = CommUtil.setResultInfo(jsonMap, Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001);
			CommUtil.responseJson(result, response);
		} finally {
			DBUtil.closeConn();
		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		this.doGet(request, response);
	}
}
